package controllers

import (
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

//
type Visitor struct {
	IsLogin      bool
	UserUserId   int64
	UserUsername string
}

type BaseController struct {
	beego.Controller
	Visitor
}

//
func (this *BaseController) Prepare() {
	userLogin := this.GetSession("userLogin")
	if userLogin == nil {
		this.IsLogin = false
	} else {
		this.IsLogin = true
		tmp := strings.Split((this.GetSession("userLogin")).(string), "||")

		userid, _ := strconv.Atoi(tmp[0])
		longid := int64(userid)

		this.UserUserId = longid
		this.UserUsername = tmp[1]

	}
	this.Data["IsLogin"] = this.IsLogin
	this.Data["LoginUserid"] = this.UserUserId
	this.Data["LoginUsername"] = this.UserUsername
}

//主页
type MainController struct {
	BaseController
}

func (this *MainController) Get() {
	if !this.IsLogin {
		this.Redirect("/login", 302)
	}
	this.TplName = "index.tpl"
}
